#ifndef __IP_H_
#define __IP_H_

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include "uint128_t.h"

#ifdef __cplusplus
extern "C" {
#endif

	typedef struct tag_ip
	{
		uint32_t prefStart[256];
		uint32_t prefEnd[256];
		uint32_t prefStart6[65600];
		uint32_t prefEnd6[65600];
		FILE* file;
		uint8_t* buffer;
		const char* readMode;
		const char* fileName;
		int version;
		long numbers;
	}geo_ip;

	geo_ip* ipv4InstanceLocationFile(const char* fileName);
	geo_ip* ipv4InstanceLocationMemory(const char* fileName);
	geo_ip* ipv4InstanceScenesFile(const char* fileName);
	geo_ip* ipv4InstanceScenesMemory(const char* fileName);
	geo_ip* ipv4InstanceProxyFile(const char* fileName);
	geo_ip* ipv4InstanceProxyMemory(const char* fileName);
	geo_ip* ipv6InstanceFile(const char* fileName);
	geo_ip* ipv6InstanceMemory(const char* fileName);
	int32_t ip_loadDat_v4(geo_ip* p, const char* fileName);
	int32_t ip_loadDat_v6(geo_ip* p, const char* fileName);
	char* ip_query(geo_ip* p, const char* ip);
	long ip_binary_search(geo_ip* p, long low, long high, long k);
	uint32_t ip_ip2long(geo_ip* p, const char* addr, uint32_t* prefix);
	uint32_t unpack_int_4byte(geo_ip* p, long offset);
	uint32_t unpack_int_1byte(geo_ip* p, long offset);
	long getPref(char* ip);
	long ipv6_binary_search(geo_ip* p, long low, long high, uint128_t k);
	char* get_addr(geo_ip* p, uint32_t j);
	char* get_addr6(geo_ip* p, uint32_t j);
	
#ifdef __cplusplus
}
#endif
#endif